from classfile.attribute_info import AttributeInfo


class LineNumberTableEntry:
    def __init__(self, startPC, lineNumber):
        self.startPC = startPC
        self.lineNumber = lineNumber


class LineNumberTableAttribute(AttributeInfo):
    def __init__(self):
        super().__init__()
        self.lineNumberTable = []

    def readInfo(self, clsReader):
        lineNumberSize = int.from_bytes(clsReader.readU16(), "big")
        for i in range(lineNumberSize):
            self.lineNumberTable.append(LineNumberTableEntry(
                int.from_bytes(clsReader.readU16(), "big"), int.from_bytes(clsReader.readU16(), "big")))

    def getLineTable(self, pc):
        for i in range(len(self.lineNumberTable)-1, -1, -1):
            entry = self.lineNumberTable[i]
            if pc > entry.startPC:
                return entry.lineNumber
        return -1
